package com.dhcc.logs.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.dhcc.logs.common.event.UserEvent;
import com.dhcc.logs.dao.UserDao;
import com.dhcc.logs.entity.User;
import com.dhcc.logs.entity.vo.UserEventLog;
import com.dhcc.logs.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
    @Autowired
    private ApplicationEventPublisher publisher;
    
	// 1、分页查询
	@Override
	public Page findAll(Map<String, Object> map, int page, int size) {
		// 1.需要查询条件
		Specification<User> spec = new Specification<User>() {
			/**
			 * 动态拼接查询条件
			 * 
			 * @return
			 */
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> list = new ArrayList<>();
				// 根据请求的companyId是否为空构造查询条件
				if (!StringUtils.isEmpty(map.get("name"))) {
					list.add(criteriaBuilder.equal(root.get("name").as(String.class), (String) map.get("name")));
				}
				return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
			}
		};
		// 2.分页
		Page<User> pageUser = userDao.findAll(spec, new PageRequest(page - 1, size));
		return pageUser;
	}

	/**
	 * 1.保存用户
	 */
	@Override
	public void save(User user) {
		user.setPassword("123456");// 设置初始密码
		// 调用dao save
		userDao.save(user);
		publisher.publishEvent(user);
	}

	/**
	 * 2.更新用户
	 */
	@Override
	public void update(User user) {
		// 1.根据id查询
		User target = userDao.findById(user.getId()).get();
		UserEventLog userEventLog = new UserEventLog(user.getId(), user.getName(), target.getName(), new Date());
		userEventLog.setContent(target.getName() + " 更新为" + user.getName());
		// 2.设置属性
		target.setName(user.getName());
		// 3.更新
		userDao.save(target);
		//监听事件
		publisher.publishEvent(new UserEvent(userEventLog));
	}

	/**
	 * 3.根据id查询用户
	 */
	@Override
	public User findById(String id) {
		User user = userDao.findById(id).get();
		return user;
	}

	/**
	 * 4.查询用户
	 */
	@Override
	public List<User> findAll() {
		return userDao.findAll();
	}

	/**
	 * 5.根据id删除用户
	 */
	@Override
	public void deleteById(String id) {
		userDao.deleteById(id);
	}

	/**
	 * 6.删除所有用户
	 */
	@Override
	public void deleteAll() {
		userDao.deleteAll();
	}

	@Override
	public User findOne(String id) {
		User user = userDao.findById(id).get();
		return user;
	}
}